

package de.gympu.gympulan.core;

import java.awt.FlowLayout;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

import org.java.plugin.boot.Application;
import org.java.plugin.boot.ApplicationPlugin;
import org.java.plugin.registry.Extension;
import org.java.plugin.registry.ExtensionPoint;
import org.java.plugin.registry.Extension.Parameter;
import org.java.plugin.util.ExtendedProperties;

import de.gympu.gympulan.core.plugins.gpldatabaseplugin.GPLDatabase;
import de.gympu.gympulan.core.plugins.gplloggingplugin.GPLLogging;
import de.gympu.gympulan.core.plugins.gplmainplugin.GPLMainPlugin;
import de.gympu.gympulan.core.plugins.gplsettingsplugin.GPLSettings;
import de.gympu.gympulan.core.pluginutil.PluginLoader;


public final class CorePlugin extends ApplicationPlugin implements Application {

	private Vector<GPLMainPlugin> runningMainPlugins = new Vector<GPLMainPlugin>();
	
	protected Application initApplication(ExtendedProperties arg0, String[] arg1)
			throws Exception {
		return this;
	}

	protected void doStart() throws Exception {
		;
	}

	protected void doStop() throws Exception {
		;
	}

	public void startApplication() throws Exception {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		}
		catch(Exception e) {}
		
		this.registerDatabase();
		
		this.registerLogging();
		
		this.registerSettings();
		
		this.registerMainPlugins();
			
	}
	

	private void registerMainPlugins() {
		PluginLoader<GPLMainPlugin> mainPloader = new PluginLoader<GPLMainPlugin>(this);
		while(mainPloader.hasNext()) {
			GPLMainPlugin plugin = mainPloader.getNext();
			plugin.startPlugin();
			this.runningMainPlugins.add(plugin);
		}
	}

	private void registerLogging() {
		PluginLoader<GPLLogging> pLoader = new PluginLoader<GPLLogging>(this);
		if(pLoader.hasNext()) {
			GPLLogging gplLogging = pLoader.getNext();
			gplLogging.init();
			
			GymPuLanImpl.gpllog = gplLogging;
		}
	}
	
	private void registerSettings() {
		PluginLoader<GPLSettings> pLoader = new PluginLoader<GPLSettings>(this);
		if(pLoader.hasNext()) {
			GPLSettings settings = pLoader.getNext();
			settings.load();
			
			GymPuLanImpl.gplsettings = settings;
		}
	}
	
	private void registerDatabase() {
		// Zugangsdaten sind in der Properties Datei
		
		Properties db = new Properties();
		
		try {
			
			FileInputStream stream = new FileInputStream("settings/database.properties");
			db.load(stream);
			stream.close();
		}
		catch(IOException e) {
			System.out.println("Schwerer Fehler: kann Einstellungen in der Datei settings/database.properties nicht lesen! (" + e.getMessage() + ")");
		}
		
		String server = db.getProperty("DBHostName");
		int port = Integer.parseInt(db.getProperty("Port"));
		String username = db.getProperty("DBUserName");
		String password = db.getProperty("DBPassword");
		String database = db.getProperty("DBDatabase");
		
		PluginLoader<GPLDatabase> pLoader = new PluginLoader<GPLDatabase>(this);
		if(pLoader.hasNext()) {
			GPLDatabase databaseObject = pLoader.getNext();
			databaseObject.connect(server, port, username, password, database);
			GymPuLanImpl.gpldb = databaseObject;
        }

	}
}
